package cn.shenlong.portal.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.shenlong.common.Page;
import cn.shenlong.portal.domain.Storetag;
import cn.shenlong.portal.service.StoretagService;

@Controller
@RequestMapping("/storetag/admin")
public class StoretagAController {
	
	@Autowired
	private StoretagService storetagService;
	
	
	@RequestMapping("/list")
	public String list(String searchProperty, String searchValue,
			Integer pageNumber, Integer pageSize, HttpServletRequest request,
			ModelMap model){
		if (pageNumber == null) {
			pageNumber = 1;
		}
		if (pageSize == null) {
			pageSize = 20;
		}
		Map<String,Object> parm = new HashMap<String,Object>();
		if(searchProperty!=null){
			parm.put(searchProperty, searchValue);
			model.put("searchValue", searchValue);
			parm.put("searchProperty", searchProperty);
			model.put("searchProperty", searchProperty);
		}
		Page<Storetag> page = storetagService.findForStoretag(parm, pageNumber, pageSize);
		
		model.put("page", page);
		
		return "/server/storetag/list";
		
	}
	@RequestMapping("/add")
	public String add() {
		return "/server/storetag/add";
	}

	@RequestMapping("/save")
	public String save(Storetag storetag,ModelMap model ){
		storetagService.saveStoretag(storetag);
		return "redirect:list.html";
	}
	
	@RequestMapping("/edit")
	public String edit(Integer id, Storetag Storetag,ModelMap model ) {
		Storetag storetag = storetagService.findStoretagById(id);
		model.put("tag", storetag);
		return "/server/storetag/edit";
	}
	
	@RequestMapping("/update")
	public String update(Storetag storetag,ModelMap model ){
		storetagService.updateStoretag(storetag);
		return "redirect:list.html";
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public Map<String, Object> delete(  String idStr,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
 
				String[] tmp = idStr.split(",");
				Integer[] ids = new Integer[tmp.length];
				for (int i = 0; i < tmp.length; i++) {
					ids[i] = Integer.parseInt(tmp[i]);
				}
				storetagService.deleteStoretag(ids);

			resultMap.put("result", "删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "删除失败！原因：" + e.toString());
		}
		return resultMap;
	}

	
	
	

}
